/** 
 * File Name:MenuController.java 
 * Date:2016年8月2日上午11:39:46 
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved. 
 * 
*/
package com.zeei.bfs.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.zeei.bfs.dao.SysLogDao;
import com.zeei.bfs.service.MenuService;
import com.zeei.bfs.vo.QueryMenuParamVO;
import com.zeei.common.annotation.SysLog;
import com.zeei.common.constant.GlobalConstant;
import com.zeei.common.controller.BasicController;
import com.zeei.common.enums.LogType;
import com.zeei.common.utils.SessionInfoUtil;
import com.zeei.common.utils.UserOperationLog;
import com.zeei.common.vo.MenuVO;
import com.zeei.common.vo.PointSysTypeVO;
import com.zeei.common.vo.ResultBasicVO;
import com.zeei.common.vo.ResultRowsVO;
import com.zeei.common.vo.TreeNodeVO;
import com.zeei.common.vo.UserVO;

@Controller
@RequestMapping("/bfs/menu")
public class MenuController extends BasicController {

	@Resource
	private MenuService menuService;

	@Resource
	private SysLogDao syslogDao;

	/**
	 * 方法描述:菜单列表
	 *
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<MenuVO>> queryMenu(QueryMenuParamVO queryMenuParam) {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		List<TreeNodeVO<MenuVO>> list = new ArrayList<TreeNodeVO<MenuVO>>();
		// 该判断提取出到公共地方
		if (GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())) {// 超级管理员使用菜单管理
			list = menuService.queryMenu(queryMenuParam);
		}
		return this.createResultRowsVO(list, true, null, list.size());
	}

	// @ResponseBody
	// @RequestMapping("/getMenuInfo")
	// public ResultRowsVO<TreeNodeVO<MenuVO>> getMenuInfo() {
	// UserVO userVO = this.getUserVO();
	// List<TreeNodeVO<MenuVO>> lsInfo =
	// menuService.queryUserMenu(userVO.getUserid());
	// return this.createResultRowsVO(lsInfo, true, null, lsInfo.size());
	// }

	/**
	 * 方法描述:角色菜单权限设置,查询角色拥有的菜单
	 *
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/listByRole", method = RequestMethod.POST)
	public ResultRowsVO<TreeNodeVO<MenuVO>> queryRoleMenu(Integer roleId, String systemType) {
		List<TreeNodeVO<MenuVO>> lsInfo = menuService.queryRoleMenu(roleId, systemType);
		ResultRowsVO<TreeNodeVO<MenuVO>> list = this.createResultRowsVO(lsInfo, true, null, lsInfo.size());
		return list;
	}

	/**
	 * 方法描述:查询第一级菜单，用于桌面之类展示
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryFirstMenu", method = RequestMethod.POST)
	public ResultRowsVO<MenuVO> queryFirstMenu() {
		UserVO userVO = this.getUserVO();
		List<MenuVO> lsInfo = menuService.queryUserFirstMenu(userVO);
		return this.createResultRowsVO(lsInfo, true, null, lsInfo.size());
	}

	@ResponseBody
	@RequestMapping(value = "/queryUserChildMenu", method = RequestMethod.POST)
	public ResultRowsVO<TreeNodeVO<MenuVO>> queryUserChildMenu(String systemType, Integer menuId) {
		List<TreeNodeVO<MenuVO>> lsInfo = menuService.queryUserChildMenu(this.getUserVO().getUserid(), menuId,
				systemType);
		return this.createResultRowsVO(lsInfo, true, null, lsInfo.size());
	}

	@ResponseBody
	@RequestMapping(value = "/rolemenu/add", method = RequestMethod.POST)
	@SysLog(text = "添加角色菜单", type = LogType.Edit)
	public ResultBasicVO addRoleMenu(Integer roleId, String menuJson) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 添加ID为" + roleId + "的角色菜单 操作.", LogType.Add));
		Integer userId = SessionInfoUtil.getSessionUserId();
		List<MenuVO> menuList = JSONArray.parseArray(menuJson, MenuVO.class);
		return this.createResultBasicVO(menuService.addRoleMenu(userId, roleId, menuList), null);
	}

	@RequestMapping(value = "/usermenu/list", method = RequestMethod.POST)
	@ResponseBody
	public List<TreeNodeVO<MenuVO>> queryUserMenu(Integer userId) {
		return menuService.queryUserMenu(userId);
	}

	@RequestMapping(value = "/usermenu/add", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "添加用户菜单", type = LogType.Edit)
	public ResultBasicVO addUserMenu(Integer userId, Integer[] menuIds) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 添加ID为"+userId+"的用户菜单 操作.", LogType.Add));
		Integer creatorId = SessionInfoUtil.getSessionUserId();
		return this.createResultBasicVO(menuService.addUserMenu(creatorId, userId, menuIds), null);
	}

	@RequestMapping(value = "/drop", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "删除菜单信息", type = LogType.Edit)
	public ResultBasicVO dropMenu(Integer[] menuIds) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 删除菜单ID为" + StringUtils.join(menuIds, ",") + "的信息 操作.", LogType.Delete));
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		if (!GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())) {
			return this.createResultBasicVO(false, "没有删除菜单权限!");
		}
		List<Integer> list = Arrays.asList(menuIds);
		return this.createResultBasicVO(menuService.dropMenu(list), null);
	}

	@RequestMapping(value = "/valid", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO validMenuName(MenuVO menu) {
		return this.createResultBasicVO(menuService.validMenuName(menu), null);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	@SysLog(text = "编辑菜单信息", type = LogType.Edit)
	public ResultBasicVO editMenu(MenuVO menu) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 编辑" + menu.getMenuName() + "菜单信息 操作.", LogType.Edit));
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		if (!GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())) {
			return this.createResultBasicVO(false, "没有修改菜单权限!");
		}
		return this.createResultBasicVO(menuService.editMenu(menu), null);
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@SysLog(text = "添加菜单", type = LogType.Edit)
	public ResultBasicVO addMenu(MenuVO menu) {
		syslogDao.insert(UserOperationLog.getSysLogVO(request, " 执行 添加" + menu.getMenuName() + "菜单信息 操作.", LogType.Add));
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		if (!GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())) {
			return this.createResultBasicVO(false, "没有添加菜单权限!");
		}
		return this.createResultBasicVO(menuService.addMenu(menu, user), null);
	}

	@RequestMapping(value = "/combobox", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<MenuVO>> queryCombobox() {
		List<TreeNodeVO<MenuVO>> menuTree = menuService.queryMenu();
		return this.createResultRowsVO(menuTree, true, null, menuTree.size());
	}

	@ResponseBody
	@RequestMapping(value = "/getSystemType", method = RequestMethod.POST)
	public List<PointSysTypeVO> categoryListByRole(Integer roleId, String categoryclass) {
		/*
		 * List<PointSysTypeVO> nodeDataList = new ArrayList<PointSysTypeVO>();
		 */
		List<PointSysTypeVO> systemTypeEntitys = menuService.getSystemTable();
		return systemTypeEntitys;
	}

}
